﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Utils.Controls;
using System.Linq;
using DevExpress.XtraBars;
using UniMana.Data;
using DevExpress.XtraPrinting;
using UniMana.Utils.Data;


namespace UniMana.Win.Training
{
    public partial class FrmStudent_SubList : DevExpress.XtraEditors.XtraForm
    {

        //1111
        bds m_bdsStudent_Class = new bds();
        DBMain m_data = new DBMain();

        //Rule cho toàn Form
        private string m_Rule = UniMana.Utils.Configs.RuleConfig.STUDENTINFO;


        private DBMain m_Score = new DBMain();
        Dictionary<string, int> dictKeys = new Dictionary<string, int>();
        string ID = "";
        string sName = "";
        int i_FormStyle = 0;

        public FrmStudent_SubList(Dictionary<string, int> dictKeys,string ID, string Name, int iStyle)
        {
            if (!SysUtils.CheckAccessByRule(m_Rule))
            {
                return;
            }

            InitializeComponent();
            SysUtils.SetRuleToXucBar(xucBar1, this.m_Rule);

            this.dictKeys = dictKeys;
            this.ID = ID;
            this.sName = Name;
            this.i_FormStyle = iStyle;

            gctStudent.DataSource = m_bdsStudent_Class;
            rluTraining_Class.GetData(m_data.m_DCReadOnly.Training_Classes);
            rluCatalogue_Status.GetData(m_data.m_DCReadOnly.Catalogue_Status);

            if (this.i_FormStyle == 2)
            {
                xucBar1.btnAddNew.Enabled = false;
            }
        }

        #region [Events]

        #region [Context Menu]
        /// <summary>
        /// Context Menu Item : Thông tin sinh viên được click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tmiStudentInfo_Click(object sender, EventArgs e)
        {
            TaskShowStudentInfo();
        }

        private void tmiViewScoreByTerm_Click(object sender, EventArgs e)
        {
            try
            {
                string studentID = grvStudent.GetFocusedRowCellValue(grvStudent.Columns["ID"]).ToString();
                DataTable _dtProgram = new DataTable();
                _dtProgram = m_Score.m_DCReadOnly.Training_TrainingPrograms.GetTrainingProgramByStudentID(m_Score.m_DCReadOnly.Training_Student_Classes,
                    studentID).ToDataTable();
                if (_dtProgram.Rows.Count >= 2)
                {
                    FrmScore_ChooseTrainingProgram f = new FrmScore_ChooseTrainingProgram(studentID);
                    f.ShowDialog();
                }
                else
                {
                    FrmStudent_SubScoresByTerm f = new FrmStudent_SubScoresByTerm(studentID, _dtProgram.Rows[0]["Training_TrainingProgramKey"].ToString().ToInt());
                    f.ShowDialog();
                }
            }
            catch { XtraMessageBox.Show("Chưa chọn sinh viên", "Lỗi"); }
        }

        private void mộtSinhViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string key = grvStudent.GetFocusedRowCellValue("Training_StudentKey").ToString();
                var q = from s in m_data.m_DCReadOnly.Training_Students
                        join scl in m_data.m_DCReadOnly.Training_Student_Classes on s.Training_StudentKey equals scl.Training_StudentKey
                        join cls in m_data.m_DCReadOnly.Training_Classes on scl.Training_ClassKey equals cls.Training_ClassKey
                        join dep in m_data.m_DCReadOnly.Catalogue_Departments on cls.Catalogue_DepartmentKey equals dep.Catalogue_DepartmentKey
                        join maj in m_data.m_DCReadOnly.Catalogue_Majors on scl.Catalogue_MajorKey equals maj.Catalogue_MajorKey
                        where s.Training_StudentKey == Guid.Parse(key)
                        select new
                        {
                            MajorName = maj.Name,
                            ClassName = cls.Name,
                            DepartmentName = dep.Name,
                            scl.ID,
                            s.LastName,
                            s.SureName,
                            s.Birthday,
                            Gender = s.Gender == true ? "Nữ" : "Nam",
                            s.PlaceOfBirth,
                            s.Address
                        };
                bds bds1 = new bds();
                bds1.DataSource = q;
                Report_Student_Certificate rpt = new Report_Student_Certificate(bds1);
                rpt.ShowPreview();
            }
            catch (Exception ex) { XtraMessageBox.Show("Chưa chọn sinh viên: \n" + ex.Message, "Lỗi"); }
        }

        private void lớpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string key = grvStudent.GetFocusedRowCellValue("Training_StudentKey").ToString();
                var q1 = from s in m_data.m_DCReadOnly.Training_Students
                         join scl in m_data.m_DCReadOnly.Training_Student_Classes on s.Training_StudentKey equals scl.Training_StudentKey
                         join cls in m_data.m_DCReadOnly.Training_Classes on scl.Training_ClassKey equals cls.Training_ClassKey
                         where s.Training_StudentKey == Guid.Parse(key)
                         select new { cls.Training_ClassKey };
                var q = from s in m_data.m_DCReadOnly.Training_Students
                        join scl in m_data.m_DCReadOnly.Training_Student_Classes on s.Training_StudentKey equals scl.Training_StudentKey
                        join cls in m_data.m_DCReadOnly.Training_Classes on scl.Training_ClassKey equals cls.Training_ClassKey
                        join dep in m_data.m_DCReadOnly.Catalogue_Departments on cls.Catalogue_DepartmentKey equals dep.Catalogue_DepartmentKey
                        join maj in m_data.m_DCReadOnly.Catalogue_Majors on scl.Catalogue_MajorKey equals maj.Catalogue_MajorKey
                        where cls.Training_ClassKey == q1.FirstOrDefault().Training_ClassKey
                        select new
                        {
                            MajorName = maj.Name,
                            ClassName = cls.Name,
                            DepartmentName = dep.Name,
                            scl.ID,
                            s.LastName,
                            s.SureName,
                            s.Birthday,
                            Gender = s.Gender == true ? "Nữ" : "Nam",
                            s.PlaceOfBirth,
                            s.Address
                        };
                bds bds1 = new bds();
                bds1.DataSource = q;
                Report_Student_Certificate rpt = new Report_Student_Certificate(bds1);
                rpt.ShowPreview();
            }
            catch (Exception ex) { XtraMessageBox.Show("Chưa chọn sinh viên: \n" + ex.Message, "Lỗi"); }

        }

        private void chuyểnKhóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    string currentKey = grvStudent.GetFocusedRowCellValue("Training_StudentKey").ToString();
            //    Frm_AcademicHandling_Transfer_Course_StudentFilter filter = new Frm_AcademicHandling_Transfer_Course_StudentFilter(currentKey);
            //    filter.m_iTranferFlag = 0;
            //    filter.ShowDialog();
            //}
            //catch { }
        }

        private void chuyểnNgànhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string currentKey = grvStudent.GetFocusedRowCellValue("Training_StudentKey").ToString();
                int majorKey = int.Parse(grvStudent.GetFocusedRowCellValue("Catalogue_MajorKey").ToString());
                int levelKey = int.Parse(grvStudent.GetFocusedRowCellValue("Catalogue_LevelKey").ToString());
                int typeKey = int.Parse(grvStudent.GetFocusedRowCellValue("Catalogue_TrainingTypeKey").ToString());
                int courseKey = int.Parse(grvStudent.GetFocusedRowCellValue("Catalogue_CourseKey").ToString());
                FrmStudent_SubAcademicHandlingTransferCourse filter = new FrmStudent_SubAcademicHandlingTransferCourse(currentKey, majorKey, levelKey, typeKey, courseKey);
                filter.ShowDialog();
            }
            catch { }
        }
        #endregion

        #region [Xuc-Events]

        /// <summary>
        /// Nút thêm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, ItemClickEventArgs e)
        {
            //////if (!UniMana.Lib.Sys.Utils.CheckAddByRule(this.m_Rule))
            //////{
            //////    xucBar1.dlg.Close();
            //////    return;
            //////}
            FrmStudent_SubInfo frmInfo = new FrmStudent_SubInfo();

            string newKey = Guid.NewGuid().ToString();
            Training_StudentInfo studentInfo = new Training_StudentInfo(newKey);
            Training_Student_CheckListInfo checkListInfo = new Training_Student_CheckListInfo(newKey);
            Training_Student_ClassInfo studentClass = new Training_Student_ClassInfo("nguyen.huu",newKey, dictKeys["MajorKey"]);
            Training_Student_InputInfo studentInput = new Training_Student_InputInfo(newKey);


            frmInfo.m_StudentInfo = studentInfo;
            frmInfo.m_CheckListInfo = checkListInfo;
            frmInfo.m_StudentClass = studentClass;
            frmInfo.m_StudentInput = studentInput;
            frmInfo.grpCommon.Enabled = true;
            frmInfo.m_Add = true; //Nút thêm mới được nhấn
            //////xucBar1.dlg.Close();
            frmInfo.ShowDialog();
            this.ProcessFilter();
        }

        /// <summary>
        /// Nút Xóa
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, ItemClickEventArgs e)
        {
            //////if (!UniMana.Lib.Sys.Utils.CheckDeleteByRule(this.m_Rule))
            //////{
            //////    return;
            //////}

            if (m_bdsStudent_Class.Count == 0)
            {
                XtraMessageBox.Show("Chưa chọn sinh viên- không thể xóa", "Thông tin sinh viên", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (TaskCheckBeforeDelete())
            {
                if (m_bdsStudent_Class.Current == null) { return; }
                ////if (this.ShowConfirm() == DialogResult.Yes)
                ////{
                ////    string currentKey = grvStudent.GetFocusedRowCellValue("Training_StudentKey").ToString();
                ////    Lib.Student.Training_StudentInfo studentInfo = new Lib.Student.Training_StudentInfo(currentKey);
                ////    studentInfo.Delete();
                ////    studentInfo.Save();
                ////    ProcessFilter();
                ////}
            }
            else
            {
                XtraMessageBox.Show("Không thể xóa sinh viên này!, Vui lòng xóa hết các thông tin về điểm trước khi xóa.", "Xóa sinh viên", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Xuất ra file Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, ItemClickEventArgs e)
        {
            ////if (!UniMana.Lib.Sys.Utils.CheckExportByRule(this.m_Rule))
            ////{
            ////    return;
            ////}

            if (m_bdsStudent_Class.Count == 0)
            {
                XtraMessageBox.Show("Không thể xuất khi số dòng dữ liệu bằng 0", "Lỗi khi in", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "*.xlsx | *.xls";
            sfd.Title = "Lưu tập tin excel 2007";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                //this.gctStudent.ExportToXlsx(sfd.FileName);
                XlsxExportOptions ExportOption = new XlsxExportOptions(TextExportMode.Value, true, true);
                this.gctStudent.ExportToXlsx(sfd.FileName);
            }
        }

        private void btnPrint_Click(object sender, ItemClickEventArgs e)
        {
            //////if (!UniMana.Lib.Sys.Utils.CheckPrintByRule(this.m_Rule))
            //////{
            //////    return;
            //////}

            if (m_bdsStudent_Class.Count > 0)
            {
                bds b = new bds();
                b.DataSource = grvStudent.DataSource;
                RptStudent rpt = new RptStudent(b);
                rpt.ShowPreview();
            }
            else
            {
                XtraMessageBox.Show("Không thể in khi số dòng dữ liệu bằng 0", "Lỗi khi in", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void xucBar1_btnImport_Click(object sender, ItemClickEventArgs e)
        {
            //////if(!UniMana.Lib.Sys.Utils.CheckImportByRule(this.m_Rule))
            //////{
            //////    return;
            //////}
            //////xucBar1.dlg.Close();
            //////new FrmInputExcelFile().ShowDialog();
        }
        #endregion

        /// <summary>
        /// Double Click treen 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grvStudent_DoubleClick(object sender, EventArgs e)
        {
            if (this.i_FormStyle == 1) //Là form thông tin sinh viên
            {
                TaskShowStudentInfo();
                return;
            }
            if (this.i_FormStyle == 4) //Là form nợ môn
            {
                string currentID = grvStudent.GetFocusedRowCellValue("ID").ToString();
                ////////new Frm_Score_StudentFail(currentID).ShowDialog();
                return;
            }
        }

        private void grvStudent_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            //Check whether the indicator cell belongs to a data row
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }



        private void FrmStudentList_Load(object sender, EventArgs e)
        {
            
            //////xucBar1.btnCancel.Visibility = BarItemVisibility.Never;
            //xucBar1.btnEdit.Visibility = BarItemVisibility.Never;
            //////xucBar1.btnSave.Visibility = BarItemVisibility.Never;
            //////xucBar1.btnRefresh.Visibility = BarItemVisibility.Never;
            //xucBar1.btnImportExcel.Visibility = BarItemVisibility.Never;
            this.ProcessFilter();
            grvStudent.BestFitColumns();
        }
        #endregion

        #region [Process]
        /// <summary>
        /// Lọc dữ liệu
        /// </summary>
        private void ProcessFilter()
        {
            switch (this.i_FormStyle)
            {
                case 1: this.TaskFilterByInfo();
                    break;
                case 2: this.TaskFilterByCheckList();
                    break;
                case 3: this.TaskFilterByMissInfo();
                    break;
                case 4: this.TaskFilterDebtSubject();
                    grcSumFail.Visible = true;
                    //grcHomePhone.Visible = false;
                    //grcPlaceOfBirth.Visible = false;
                    //grcStatus.Visible = false;
                    break;
                default: 
                    return;
            }

            //Ẩn các thông tin không thiết trên lưới
            if (this.i_FormStyle != 4)
            {
                grcSumFail.Visible = false;
                //grcHomePhone.Visible = true;
                //grcPlaceOfBirth.Visible = true;
                //grcStatus.Visible = true;
            }
        }
        #endregion

        #region [Task]

        /// <summary>
        /// Hiển thị Form thông tin sinh viên
        /// </summary>
        private void TaskShowStudentInfo()
        {
            try
            {
                string currentKey = grvStudent.GetFocusedRowCellValue("Training_StudentKey").ToString();
                int currentMajor = int.Parse(grvStudent.GetFocusedRowCellValue("Catalogue_MajorKey").ToString());
                FrmStudent_SubInfo frmInfo = new FrmStudent_SubInfo();
                Training_StudentInfo studentInfo = new Training_StudentInfo(currentKey);
                Training_Student_CheckListInfo studentCheckListInfo = new Training_Student_CheckListInfo(currentKey);
                Training_Student_ClassInfo studentClass = new Training_Student_ClassInfo("nguyen.huu", currentKey, currentMajor);
                Training_Student_InputInfo studentInput = new Training_Student_InputInfo(currentKey);

                frmInfo.m_StudentInfo = studentInfo;
                frmInfo.m_CheckListInfo = studentCheckListInfo;
                frmInfo.m_StudentClass = studentClass;
                frmInfo.m_StudentInput = studentInput;


                frmInfo.ShowDialog();
                this.ProcessFilter();
            }
            catch (Exception ex) { XtraMessageBox.Show("Lỗi.\n Mã lỗi:" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        /// <summary>
        /// Lọc danh sách sinh viên
        /// </summary>
        private void TaskFilterByInfo()
        {
            if (this.ID == "" && this.sName == "")
            {
                m_bdsStudent_Class.DataSource =
                                m_data.m_DCReadOnly.Training_Students.GetListByRef
                                (
                                m_data.m_DCReadOnly.Training_Student_Classes,
                                dictKeys["LevelKey"], dictKeys["TypeKey"],
                                dictKeys["CourseKey"], dictKeys["DepartmentKey"],
                                dictKeys["MajorKey"], dictKeys["StatusKey"], dictKeys["ClassKey"]);
                return;
            }
            else if (this.ID != "")
            {
                m_bdsStudent_Class.DataSource = m_data.m_DCReadOnly.Training_Students.GetListLikeID(m_data.m_DCReadOnly.Training_Student_Classes, this.ID, dictKeys["DepartmentKey"]);
                return;
            }
            else
            {
                m_bdsStudent_Class.DataSource = m_data.m_DCReadOnly.Training_Students.GetListLikeName(m_data.m_DCReadOnly.Training_Student_Classes, this.sName, dictKeys["DepartmentKey"]);
                return;
            }
        }

        /// <summary>
        /// Lọc hồ sơ thiếu
        /// </summary>
        private void TaskFilterByCheckList()
        {
            if (this.ID == "" && this.sName == "")
            {
                gctStudent.DataSource = m_data.m_DCReadOnly.Training_Student_Checklists.GetListDebtProfile(m_data.m_DCReadOnly.Training_Students,
                                                                                         m_data.m_DCReadOnly.Training_Student_Classes,
                                                                                         dictKeys["LevelKey"], dictKeys["TypeKey"],
                                                                                         dictKeys["CourseKey"], dictKeys["DepartmentKey"],
                                                                                         dictKeys["MajorKey"], dictKeys["ClassKey"]);
                return;
            }
            else if (ID != "")
            {
                gctStudent.DataSource = m_data.m_DCReadOnly.Training_Student_Checklists.GetListDebtProfileByID(m_data.m_DCReadOnly.Training_Students,
                                                                                m_data.m_DCReadOnly.Training_Student_Classes,
                                                                                ID);
                return;
            }
            else
            {
                gctStudent.DataSource = m_data.m_DCReadOnly.Training_Student_Checklists.GetListDebtProfileByName(m_data.m_DCReadOnly.Training_Students,
                                                                             m_data.m_DCReadOnly.Training_Student_Classes,
                                                                             sName);
                return;
            }
        }

        /// <summary>
        /// Lọc thông tin thiếu
        /// </summary>
        private void TaskFilterByMissInfo()
        {
            if (this.ID == "" && this.sName == "")
            {
                gctStudent.DataSource = m_data.m_DCReadOnly.Training_Students.GetListMissInfoByRef(m_data.m_DCReadOnly.Training_Student_Classes,
                                                                                         dictKeys["LevelKey"], dictKeys["TypeKey"],
                                                                                         dictKeys["CourseKey"], dictKeys["DepartmentKey"],
                                                                                         dictKeys["MajorKey"], dictKeys["ClassKey"]);
                return;
            }
            else if (ID != "")
            {
                gctStudent.DataSource = m_data.m_DCReadOnly.Training_Students.GetListMissInfoLikeID(
                                                                                m_data.m_DCReadOnly.Training_Student_Classes,
                                                                                ID);
                return;
            }
            else
            {
                gctStudent.DataSource = m_data.m_DCReadOnly.Training_Students.GetListMissInfoLikeName(
                                                                             m_data.m_DCReadOnly.Training_Student_Classes,
                                                                             sName);
                return;
            }
        }

        private void TaskFilterDebtSubject()
        {
            var q = m_data.m_DCReadOnly.Training_Classes.GetSubjectClassFail(
                                                                        m_data.m_DCReadOnly.Training_Student_Classes,
                                                                        m_data.m_DCReadOnly.Training_Students,
                                                                        m_data.m_DCReadOnly.Training_Subject_Classes,
                                                                        m_data.m_DCReadOnly.Training_Student_Subject_Classes,
                                                                        m_data.m_DCReadOnly.Training_Score_Transfers,
                                                                        m_data.m_DCReadOnly.Training_Scores,
                                                                        dictKeys["LevelKey"], dictKeys["TypeKey"],
                                                                        dictKeys["CourseKey"], dictKeys["DepartmentKey"],
                                                                        dictKeys["MajorKey"], dictKeys["ClassKey"]);

            DataTable dt = new DataTable();
            dt=q.ToDataTable();

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string strStudentKey = dt.Rows[i]["Training_StudentKey"].ToString();
                    string strSubjectKey = dt.Rows[i]["Training_SubjectKey"].ToString();
                    double dbScore = dt.Rows[i]["AverageScoreFinal"].ToDouble();
                    for (int j = i + 1; j < dt.Rows.Count; j++)
                    {
                        string strStudentKey1 = dt.Rows[j]["Training_StudentKey"].ToString();
                        string strSubjectKey1 = dt.Rows[j]["Training_SubjectKey"].ToString();
                        if (strStudentKey == strStudentKey1 && strSubjectKey == strSubjectKey1)
                        {
                            dt.Rows.RemoveAt(j);
                            break;
                        }
                    }

                    if (bool.Parse(dt.Rows[i]["Yearly"].ToString()))
                    {
                        if (dbScore >= UniMana.Utils.Configs.DefaultConfig.MinScorePass_Yearly)
                        {
                            dt.Rows.RemoveAt(i);
                            i--;
                        }
                    }
                    else
                    {
                        if (dbScore >= UniMana.Utils.Configs.DefaultConfig.MinScorePass_NoYearly)
                        {
                            dt.Rows.RemoveAt(i);
                            i--;
                        }
                    }
                }

                DataTable dtDraw = new DataTable();
                dtDraw.Columns.Add("Training_StudentKey", typeof(string));
                dtDraw.Columns.Add("ID", typeof(string));
                dtDraw.Columns.Add("LastName", typeof(string));
                dtDraw.Columns.Add("SureName", typeof(string));
                dtDraw.Columns.Add("Gender", typeof(string));
                dtDraw.Columns.Add("Birthday", typeof(string));
                dtDraw.Columns.Add("CellPhone", typeof(string));
                dtDraw.Columns.Add("Email", typeof(string));
                dtDraw.Columns.Add("SumSubjectFail", typeof(int));
                dtDraw.Columns.Add("Training_TrainingProgramKey", typeof(int));
                dtDraw.Columns.Add("Training_ClassKey", typeof(int));
                dtDraw.Columns.Add("Catalogue_MajorKey", typeof(int));

                while (dt.Rows.Count > 0)
                {
                    string strStudentKey = dt.Rows[0]["Training_StudentKey"].ToString();
                    string strID = dt.Rows[0]["ID"].ToString();
                    string strLastName = dt.Rows[0]["LastName"].ToString();
                    string strSureName = dt.Rows[0]["SureName"].ToString();
                    string strGender = dt.Rows[0]["Gender"].ToString();
                    string strBirthday = dt.Rows[0]["Birthday"].ToString();
                    string strCellPhone = dt.Rows[0]["CellPhone"].ToString();
                    string strEmail = dt.Rows[0]["Email"].ToString();
                    int iTrainingProgramKey = dt.Rows[0]["Training_TrainingProgramKey"].ToInt();
                    int strSumSubjectFail = 1;
                    int classKey = dt.Rows[0]["Training_ClassKey"].ToInt();
                    int majorKey = dt.Rows[0]["Catalogue_MajorKey"].ToInt();
                    dtDraw.Rows.Add(strStudentKey, strID, strLastName, strSureName, strGender, strBirthday, strCellPhone, strEmail, strSumSubjectFail, iTrainingProgramKey,classKey, majorKey);
                    for (int i = 1; i < dt.Rows.Count; i++)
                    {
                        string strStudentKey1 = dt.Rows[i]["Training_StudentKey"].ToString();
                        if (strStudentKey == strStudentKey1)
                        {
                            strSumSubjectFail++;
                            dt.Rows.RemoveAt(i);
                            i--;
                        }
                        dtDraw.Rows[dtDraw.Rows.Count - 1]["SumSubjectFail"] = strSumSubjectFail;
                    }
                    dt.Rows.RemoveAt(0);
                }
                for (int i = 0; i < dtDraw.Rows.Count; i++)
                {
                    string strBirthday = dtDraw.Rows[i]["Birthday"].ToString();
                    string[] strSplit = strBirthday.Split('/');
                    if (strSplit.Length == 3)
                    {
                        int day = strSplit[0].ToInt();
                        int month = strSplit[1].ToInt();
                        if (day != 0 && month != 0)
                        {
                            string strDay = day > 9 ? day.ToString() : "0" + day;
                            string strMonth = month > 9 ? month.ToString() : "0" + month;
                            dtDraw.Rows[i]["Birthday"] = strDay + "/" + strMonth + "/" + strSplit[2];
                        }
                    }
                }
                gctStudent.DataSource = dtDraw;


            }
             
             
        }

        /// <summary>
        /// Kiểm tra dữ liệu trước khi xóa
        /// </summary>
        /// <returns></returns>
        public bool TaskCheckBeforeDelete()
        {
            string currentKey = grvStudent.GetFocusedRowCellValue("Training_StudentKey").ToString();
            try
            {
                if (m_data.m_DCReadOnly.Training_Student_Classes.Where(p => p.Training_StudentKey == Guid.Parse(currentKey)
                                                                       && p.ID != null).Count() > 0
                    || m_data.m_DCReadOnly.Training_Scores.Where(p => p.Training_StudentKey == Guid.Parse(currentKey)).Count() > 0
                    )
                {
                    return false;
                }
            }
            catch { return false; }
            return true;

        }

        #endregion

        private void buộcThôiHọcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string currentKey = grvStudent.GetFocusedRowCellValue("Training_StudentKey").ToString();
                int majorKey = int.Parse(grvStudent.GetFocusedRowCellValue("Catalogue_MajorKey").ToString());
                int levelKey = int.Parse(grvStudent.GetFocusedRowCellValue("Catalogue_LevelKey").ToString());
                int typeKey = int.Parse(grvStudent.GetFocusedRowCellValue("Catalogue_TrainingTypeKey").ToString());
                int courseKey = int.Parse(grvStudent.GetFocusedRowCellValue("Catalogue_CourseKey").ToString());
                //////new FrmLog_ForceCanceled(currentKey, majorKey, levelKey, typeKey, courseKey).ShowDialog();
            }
            catch { }
        }

        private void xóaHồSơToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string currentKey = grvStudent.GetFocusedRowCellValue("Training_StudentKey").ToString();
            int majorKey = int.Parse(grvStudent.GetFocusedRowCellValue("Catalogue_MajorKey").ToString());
            int levelKey = int.Parse(grvStudent.GetFocusedRowCellValue("Catalogue_LevelKey").ToString());
            int typeKey = int.Parse(grvStudent.GetFocusedRowCellValue("Catalogue_TrainingTypeKey").ToString());
            int courseKey = int.Parse(grvStudent.GetFocusedRowCellValue("Catalogue_CourseKey").ToString());
            //////new FrmLog_RemoveProfile(currentKey, majorKey, levelKey, typeKey, courseKey).ShowDialog();
        }
    }
}